//index.js
//获取应用实例
const app = getApp();
const HomeModule = require('../../modules/HomeModule');
Page({
  data: {
    musics: [],
    offset: "",
    flag:false,
    length:""
  },
  async onLoad() {
    var res = await HomeModule.getMusic({
      offset: 0
    });
    console.log(res);
    var musics = [];
    var length;
    var data = res.data.playlists;
    var total = res.data.total;
    data.forEach(element => {
      var obj = {};
      obj.pic = element.coverImgUrl;
      obj.name = element.name;
      obj.author = element.description;
      obj.people = element.playCount;
      musics.push(obj);
    });
    length=musics.length
    this.setData({
      musics,
      total,
      length
    })
  },
  handleDro(event) {
    var thisIndex = event.target.dataset.index;
    var musics = this.data.musics;
    console.log(thisIndex)
    musics.splice(thisIndex, 1);
    this.setData({
      musics
    })
  },
 
  async onReachBottom() {
    var length = this.data.length;
    var flag=this.data.flag;
    if (flag==false) {
      var res = await HomeModule.getMusic({
        offset: length
      })
      var data = res.data.playlists;
      var length=this.data.length;
      var newMusics=[];
      length=data.length+length;
      if(length==100){
        flag=true;
      }
     data.forEach(element => {
        var obj = {};
        obj.pic = element.coverImgUrl;
        obj.name = element.name;
        obj.author = element.description;
        obj.people = element.playCount;
        newMusics.push(obj);
      });
      this.setData({
        musics:[...this.data.musics,...newMusics],
        length,
        flag
      })
    }else{
      console.log("到达底部");
    
      wx.showToast({
        title: '到底',
        icon: 'success',
        duration: 1000,
        mask: true,
        success: function() {
          setTimeout(function() {
            //要延时执行的代码
          
          }, 1000) //延迟时间
        },
      });
          
    } 
  }
  
})